package com.ccbooks.util;

import java.io.IOException;

import android.util.Log;
import android.widget.Toast;

import com.ccbooks.bo.FileBo;
import com.ccbooks.util.PDB.BooksEnginePDB;
import com.ccbooks.util.PDB.ContentDispPDB;
import com.ccbooks.util.PDB.ContentUtilBookMarkPDB;
import com.ccbooks.util.PDB.ContentUtilFirstPDB;
import com.ccbooks.util.PDB.ContentUtilNextPDB;
import com.ccbooks.util.PDB.ContentUtilPDB;
import com.ccbooks.util.PDB.ContentUtilReturnPDB;
import com.ccbooks.util.PDB.ContentUtilZeroPDB;
import com.ccbooks.view.BookContentView;

public class CursorController {
	public BooksEngine booksEngine;
	public BooksEnginePDB booksEnginePDB;
	public int cursor = 0;
	public String content;
	public ContentUtil cu; // 分页类
	public ContentDisp cd;
	public ContentUtilReturn cur;
	public ContentUtilNext cun;
	public ContentUtilZero cuz;
	public ContentUtilBookMark cubm;
	public ContentUtilFirst cuf;
	public ContentUtilPDB cuPDB; // 分页类
	public ContentDispPDB cdPDB;
	public ContentUtilReturnPDB curPDB;
	public ContentUtilNextPDB cunPDB;
//	public ContentUtilZeroPDB cuzPDB;
	public ContentUtilBookMarkPDB cubmPDB;
	public ContentUtilFirstPDB cufPDB;
	public ContentUtilZeroPDB cuzPDB;
	public int lineIndex = 0; // 开头行号
	public int lineCount;	//	总行数；
	public BookContentView bcv;
	private final static int MAX_BUF_SIZE = 1024*100;
	
	ContentUtilContentSize cucs;
	
	public CursorController(BooksEngine booksEngine, ContentUtil cu,
			ContentDisp cd, ContentUtilReturn cur, 
			BookContentView bcv,ContentUtilFirst cuf) {
		super();
		this.booksEngine = booksEngine;
		this.cu = cu;
		this.cd = cd;
		this.cur = cur;
		this.cuf = cuf;
		this.bcv = bcv;
	}
	public CursorController(BooksEnginePDB booksEnginePDB, ContentUtilPDB cuPDB,
			ContentDispPDB cdPDB, BookContentView bcv,ContentUtilFirstPDB cufPDB) {
		super();
		this.booksEnginePDB = booksEnginePDB;
		this.cuPDB = cuPDB;
		this.cdPDB = cdPDB;
		this.cufPDB = cufPDB;
		this.bcv = bcv;
	}
	public CursorController(BooksEngine booksEngine, 
			ContentDisp cd, ContentUtilReturn cur, 
			BookContentView bcv,ContentUtilFirst cuf) {
		super();
		this.booksEngine = booksEngine;
		
		this.cd = cd;
		this.cur = cur;
		this.cuf = cuf;
		this.bcv = bcv;
	}


	public int getLineIndex() {
		return lineIndex;
	}
	

	public void setLineIndex(int lineIndex) {
		this.lineIndex = lineIndex;
	}

	
	
	
	/**取得下一页内容*/
	public String nextPage() throws IndexOutOfBoundsException, IOException{
		
		/**判断文件是否翻到末尾*/
		int fontSize  = TextSizeUtil.fontSize;
		TextSizeUtil.fontSize=fontSize;
		lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
		lineIndex += lineCount;
		if(FileBo.BOOKTYPE_TXT.equals(bcv.book.booktype)){
		
		
		if(lineIndex == (BooksEngine.curLine-5*lineCount)){
			
			
			if(bcv.portraint ==1){
				cun = new ContentUtilNext(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK",bcv.cu.engine,bcv.portraint);	
				
			}else if(bcv.portraint ==2){
				cun = new ContentUtilNext(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK",bcv.cu.engine,bcv.portraint);
				
			}		
			
			
			Thread th = new Thread(cun);
			th.start();
			
		}
	
		
		 bcv.cd.setLine(lineIndex, lineCount);
		content =  bcv.cd.secThread();
	
		if (content == null) {
			bcv.lastPageShowing();
			lineIndex = lineIndex-lineCount;
			bcv.cd.setLine(lineIndex, lineCount);
			content =   bcv.cd.secThread();
			
		}
	}else if(FileBo.BOOKTYPE_PDB.equals(bcv.book.booktype)){
		if(lineIndex == (BooksEnginePDB.curLine-5*lineCount)){
			
			
			if(bcv.portraint ==1){
				cunPDB = new ContentUtilNextPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK",bcv.cuPDB.engine,bcv.portraint);	
				
			}else if(bcv.portraint ==2){
				cunPDB = new ContentUtilNextPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK",bcv.cuPDB.engine,bcv.portraint);
				
			}		
			
			
			Thread th = new Thread(cunPDB);
			th.start();
			
		}
	
		
		 bcv.cdPDB.setLine(lineIndex, lineCount);
		content =  bcv.cdPDB.secThread();
	
		if (content == null) {
			
			lineIndex = lineIndex-lineCount;
			bcv.cdPDB.setLine(lineIndex, lineCount);
			content =   bcv.cdPDB.secThread();
			
		}
	}else if(FileBo.BOOKTYPE_CHM.equals(bcv.book.booktype)){
		
	}else if(FileBo.BOOKTYPE_HTML.equals(bcv.book.booktype)){
		
	}else if(FileBo.BOOKTYPE_PDF.equals(bcv.book.booktype)){
		
	}
		return content;
	}
	
	/**取得上一页内容*/
	public String previousPage() throws IndexOutOfBoundsException, IOException{
	
		int fontSize  = TextSizeUtil.fontSize;
		TextSizeUtil.fontSize=fontSize;
		lineCount = TextSizeUtil.getLineCount(fontSize, bcv.portraint);
		
		lineIndex -= lineCount;
		System.out.println("lineIndex------->"+lineIndex);
	//	System.out.println("booksEngine.status------->"+bcv.cu.engine.status);

		
		if(FileBo.BOOKTYPE_TXT.equals(bcv.book.booktype)){	
			if(lineIndex > 0){
		
//				 if (lineIndex == 5*lineCount) {
//						if(bcv.cu.engine.status == 3){
//						cur = new ContentUtilReturn(bcv,bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK", bcv.portraint,bcv.cu.engine);
//						cur.setLine(0, lineCount);
//						cur.what = 1;
//						Thread th = new Thread(cur);
//						th.start();
//							
//							
//						}
//					}
				
				 bcv.cd.setLine(lineIndex, lineCount);
						
					content = bcv.cd.secThread();
			 }
			if(lineIndex <= 0){
				if(bcv.portraint ==1){
					cur = new ContentUtilReturn(bcv,bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK", bcv.portraint,bcv.cu.engine);
				}else if(bcv.portraint == 2){
					cur = new ContentUtilReturn(bcv,bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK", bcv.portraint,bcv.cu.engine);
				}
				
				
				cur.what = 1;
				cur.firstThread();
					
				
				
				System.out.println("getPageContent2");
			
				int[] tempArray = new int[MAX_BUF_SIZE];
				int temp = bcv.cu.engine.curLine2;
				System.out.println("temp"+temp);
				
				int pageCount = temp/lineCount;
				int remainder = temp%lineCount;
				int pageIndex = remainder+pageCount*(lineCount);
				
				if(temp < lineCount){
					lineIndex = 0;
					System.out.println("---ZERO---------->end");
					if(bcv.portraint == 1){
						cuz = new ContentUtilZero(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK", bcv.cu.engine);
					}else if(bcv.portraint == 2){
						cuz = new ContentUtilZero(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK", bcv.cu.engine);
					}
					
					cuz.setLine(0, lineCount);
					cur.what = 1;
					Thread th = new Thread(cuz);
					th.start();
					content = cuz.secThread();	
						System.out.println("contentThread---------->end");
					return content;
				}
				
				tempArray[0] = 0;
				//
				System.arraycopy(bcv.cu.engine.lineIndex2, remainder, tempArray, 0,pageCount*(lineCount));
				for(int i=0;i<bcv.cu.engine.curLine2+2;i++){
					System.out.println("index2="+i+"="+bcv.cu.engine.lineIndex2[i]);
				}
				for(int i=0;i<bcv.cu.engine.curLine+2;i++){
					System.out.println("index="+i+"="+bcv.cu.engine.lineIndex[i]);
				}
				for(int i=0;i<bcv.cu.engine.curLine2+2;i++){
					System.out.println("tempArray="+i+"="+tempArray[i]);
				}
				
				System.arraycopy(bcv.cu.engine.lineIndex, 0, tempArray, pageCount*(lineCount), bcv.cu.engine.curLine+pageCount*(lineCount));
				for(int i=0;i<bcv.cu.engine.curLine+pageCount*(lineCount)+2;i++){
					System.out.println("tempArray2="+i+"="+tempArray[i]);
				}
				
				System.arraycopy(tempArray, 0, bcv.cu.engine.lineIndex, 0, bcv.cu.engine.curLine+pageCount*(lineCount)+1);
				bcv.cd.engine.status = 3;
				bcv.cu.engine.pageMarkNum = bcv.cu.engine.lineIndex[0];
				for(int i=0;i<bcv.cu.engine.curLine+pageCount*(lineCount)+2;i++){
					System.out.println("lineIndex="+i+"="+bcv.cu.engine.lineIndex[i]);
				}
				
				System.out.println("-------pageMarkNum----------"+bcv.cd.engine.pageMarkNum);
				bcv.cu.engine.curLine = bcv.cu.engine.curLine+pageCount*(lineCount);
			
				lineIndex = (pageCount-1)*(lineCount);
				System.out.println("-------lineIndex----------"+lineIndex);
				bcv.cd.setLine(lineIndex, lineCount);
				
				content = bcv.cd.secThread();
				
				return content;
					
			}
		
	}else if(FileBo.BOOKTYPE_PDB.equals(bcv.book.booktype)){
		if(lineIndex > 0){
			
			 bcv.cdPDB.setLine(lineIndex, lineCount);
					
				content = bcv.cdPDB.secThread();
		 }
		if(lineIndex <= 0){
			if(bcv.portraint == 1){
				curPDB = new ContentUtilReturnPDB(bcv,bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK", bcv.portraint,bcv.cuPDB.engine);
			}else if(bcv.portraint == 2){
				curPDB = new ContentUtilReturnPDB(bcv,bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK", bcv.portraint,bcv.cuPDB.engine);
			}
			curPDB.what = 1;
			curPDB.firstThread();
				
			
			
			System.out.println("getPageContent2");
		
			int[] tempArray = new int[MAX_BUF_SIZE];
			int temp = bcv.cuPDB.engine.curLine2;
			System.out.println("temp"+temp);
			
			int pageCount = temp/lineCount;
			int remainder = temp%lineCount;
			int pageIndex = remainder+pageCount*(lineCount);
			
			if(temp < lineCount){
				lineIndex = 0;
				System.out.println("---ZERO---------->end");
				if(bcv.portraint == 1){
					cuzPDB = new ContentUtilZeroPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.VWIDE, "GBK", bcv.cuPDB.engine);
				}else if(bcv.portraint == 2){
					cuzPDB = new ContentUtilZeroPDB(bcv, bcv.book.bookpath, "宋体", StringConfig.HWIDE, "GBK", bcv.cuPDB.engine);
				}
				cuzPDB.setLine(0, lineCount);
				cuzPDB.what = 4;
				Thread th = new Thread(cuzPDB);
				th.start();
				content = cuzPDB.secThread();	
					System.out.println("contentThread---------->end");
				return content;
			}
			
			tempArray[0] = 0;
			//
			System.arraycopy(bcv.cuPDB.engine.lineIndex2, remainder, tempArray, 0,pageCount*(lineCount));
			for(int i=0;i<bcv.cuPDB.engine.curLine2+2;i++){
				System.out.println("index2="+i+"="+bcv.cuPDB.engine.lineIndex2[i]);
			}
			for(int i=0;i<bcv.cuPDB.engine.curLine+2;i++){
				System.out.println("index="+i+"="+bcv.cuPDB.engine.lineIndex[i]);
			}
			for(int i=0;i<bcv.cuPDB.engine.curLine2+2;i++){
				System.out.println("tempArray="+i+"="+tempArray[i]);
			}
	
			System.arraycopy(bcv.cuPDB.engine.lineIndex, 0, tempArray, pageCount*(lineCount), bcv.cuPDB.engine.curLine+pageCount*(lineCount));
			for(int i=0;i<bcv.cuPDB.engine.curLine+pageCount*(lineCount)+2;i++){
				System.out.println("tempArray2="+i+"="+tempArray[i]);
			}
			
			System.arraycopy(tempArray, 0, bcv.cuPDB.engine.lineIndex, 0, bcv.cuPDB.engine.curLine+pageCount*(lineCount)+1);
			bcv.cuPDB.engine.status = 3;
			bcv.cuPDB.engine.pageMarkNum = bcv.cuPDB.engine.lineIndex[0];
			for(int i=0;i<bcv.cuPDB.engine.curLine+pageCount*(lineCount)+2;i++){
				System.out.println("lineIndex="+i+"="+bcv.cuPDB.engine.lineIndex[i]);
			}
			
			System.out.println("-------pageMarkNum----------"+bcv.cuPDB.engine.pageMarkNum);
			bcv.cuPDB.engine.curLine = bcv.cuPDB.engine.curLine+pageCount*(lineCount);
		
			lineIndex = (pageCount-1)*(lineCount);
			System.out.println("-------lineIndex----------"+lineIndex);
			bcv.cdPDB.setLine(lineIndex, lineCount);
			
			content = bcv.cdPDB.secThread();
			
			return content;
				
		}
	

	}else if(FileBo.BOOKTYPE_CHM.equals(bcv.book.booktype)){
		
	}else if(FileBo.BOOKTYPE_HTML.equals(bcv.book.booktype)){
		
	}else if(FileBo.BOOKTYPE_PDF.equals(bcv.book.booktype)){
		
	}
			
					
			/*if (content == null) {
				
				lineIndex = lineIndex+lineCount;
				 bcv.cd.setLine(lineIndex, lineCount);
				content = bcv.cd.secThread();
				
			}*/
			return content;
		} 
}
	
		
	

